//
//  AddCommentViewController.m
//  Sydeny
//
//  Created by Rainbow on 8/13/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "AddCommentViewController.h"
#import "ServiceHelper.h"
#import "CacheData.h"
#import "ITSActivityIndicator.h"
#import "UtilHelper.h"

@implementation AddCommentViewController
@synthesize textView;
@synthesize commentBtn;
@synthesize shopId;
@synthesize commentBgView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [textView release];
    [commentBtn release];
    [shopId release];
    [commentBgView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"评论";
    commentBgView.layer.cornerRadius = 8;
    
    [textView becomeFirstResponder];
    
    UIButton *backBtn = [[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)] autorelease];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back_btn.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(tapBackBtn) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setTitle:@"返回" forState:UIControlStateNormal];
    [backBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
    backBtn.titleLabel.font = [UIFont boldSystemFontOfSize:12];
    UIBarButtonItem *backItem = [[[UIBarButtonItem alloc] initWithCustomView:backBtn] autorelease];
    self.navigationItem.leftBarButtonItem = backItem;
    
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setTextView:nil];
    [self setCommentBtn:nil];
    [self setCommentBgView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


-(void)tapBackBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)tapOnCommentBtn:(id)sender {
    NSMutableDictionary *params = [[[NSMutableDictionary alloc] init] autorelease];
    [params setObject:shopId forKey:@"shopid"];
    [params setObject:textView.text forKey:@"content"];
    [params setObject:[CacheData sharedData].user.userId forKey:@"uid"];
    [[ServiceHelper sharedService] requestServerForMethod:SydenyRequestAddComments WithParameters:params AndCallBackScope:self AndSuccessSel:@"addCommentSuccess:" AndFailedSel:@"addCommentFailed:"];
}

- (void)addCommentSuccess:(NSObject *)result
{
    NSDictionary *dic = (NSDictionary *)result;
    NSString *code = [UtilHelper getStringValue:[dic objectForKey:@"success"]];
    if ([@"0" isEqualToString:code]) {
        [[ITSActivityIndicator currentIndicator] displayCompleted:@"发送评论成功！"];
    }
    else
    {
        [[ITSActivityIndicator currentIndicator] displayFailed:@"发送评论失败！"];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)addCommentFailed:(NSObject *)result
{
    
    [[ITSActivityIndicator currentIndicator] displayCompleted:@"发送评论失败！"];
}

@end
